
package com.tools.cleanmaster.common.imagecache;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.utils.BitmapUtils;

public class ImageRequestApkFile extends ImageRequest {
    private static int sBitmapWidth = 0;// 缩略图宽

    private static int sBitmapHeight = 0;// 缩略图高

    public ImageRequestApkFile(String key) {
        super(key);
        if (sBitmapWidth == 0) {
            sBitmapWidth = sBitmapHeight = CleanApplication.getInstance().getResources()
                    .getDimensionPixelSize(R.dimen.trash_second_list_icon_size);
        }
    }

    @Override
    public Bitmap loadBitmap() {
        Drawable appIcon = getAppIcon(key);
        if (appIcon == null) {
            return null;
        }
        Bitmap bitmap = drawable2Bitmap(appIcon);
        if (bitmap == null) {
            return null;
        }
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        if (bitmap.getWidth() == sBitmapWidth && bitmap.getHeight() == sBitmapHeight) {
            return bitmap;
        }
        // scale bitmap
        if (bitmapWidth >= bitmapHeight) {
            bitmapHeight = (sBitmapHeight * bitmapHeight) / bitmapWidth;
            bitmapWidth = sBitmapWidth;
        } else {
            bitmapWidth = (sBitmapWidth * bitmapWidth) / bitmapHeight;
            bitmapHeight = sBitmapHeight;
        }
        return BitmapUtils.scaleBitmap(bitmap, bitmapWidth, bitmapHeight, Config.ARGB_8888, false);
    }

    private static Drawable getAppIcon(String apkFilepath) {
        PackageManager pm = CleanApplication.getInstance().getPackageManager();
        PackageInfo pkgInfo = null;
        try {
            pkgInfo = pm.getPackageArchiveInfo(apkFilepath, 0);
        } catch (Exception e) {
            // should be something wrong with parse
            e.printStackTrace();
        }
        if (pkgInfo == null) {
            return null;
        }

        // Workaround for http://code.google.com/p/android/issues/detail?id=9151
        ApplicationInfo appInfo = pkgInfo.applicationInfo;
        if (Build.VERSION.SDK_INT >= 8) {
            appInfo.sourceDir = apkFilepath;
            appInfo.publicSourceDir = apkFilepath;
        }

        return pm.getApplicationIcon(appInfo);
    }

    private static Bitmap drawable2Bitmap(Drawable drawable) {
        if (drawable == null) {
            return null;
        }

        /**
         * In that case, we cannot release the returned Bitmap anymore.
         */
        if (drawable instanceof BitmapDrawable) {
            Bitmap tmp = ((BitmapDrawable) drawable).getBitmap();
            if (tmp != null) {
                return tmp;
            }
        }

        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        if (width <= 0 || height <= 0) {
            // No intrinsic width/height, such as a solid color
            return null;
        }

        Drawable clone = drawable.getConstantState().newDrawable();
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                clone.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        clone.setBounds(0, 0, width, height);
        clone.draw(canvas);
        return bitmap;
    }
}
